//
//  MOAlertBaseView.m
//  emmoDiary
//
//  Created by miki on 2020/3/22.
//  Copyright © 2020 miki. All rights reserved.
//

#import "MOAlertBaseView.h"

@interface MOAlertBaseView()

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *detailTitle;
@property (nonatomic, strong) NSString *leftButtonImageName;
@property (nonatomic, strong) NSString *rightButtonImageName;
@property (nonatomic, assign) NSInteger style;

@end

@implementation MOAlertBaseView

+ (instancetype)alertWithTitle:(NSString *)title
                   detailTitle:(NSString *)detailTitle
                   contentView:(UIView *)contentView
                     leftTitle:(NSString *)leftTitle
                    rightTitle:(NSString *)rightTitle {
    return [[self alloc] initWithTitle:title
                            detailTitle:detailTitle
                            contentView:contentView
                    leftButtonImageName:leftTitle
                   rightButtonImageName:rightTitle
                                   style:1
                            autoDissmiss:YES
                             tapDissmiss:YES];
}

- (instancetype)initWithTitle:(NSString *)title
                  detailTitle:(NSString *)detailTitle
                  contentView:(UIView *)contentView
          leftButtonImageName:(NSString *)leftButtonImageName
         rightButtonImageName:(NSString *)rightButtonImageName
                 autoDissmiss:(BOOL)autoDissmiss
                  tapDissmiss:(BOOL)tapDissmiss {
    return [self initWithTitle:title
                    detailTitle:detailTitle
                    contentView:contentView
            leftButtonImageName:leftButtonImageName
           rightButtonImageName:rightButtonImageName
                           style:0
                    autoDissmiss:autoDissmiss
                     tapDissmiss:tapDissmiss];
}

- (instancetype)initWithTitle:(NSString *)title
                  detailTitle:(NSString *)detailTitle
                  contentView:(UIView *)contentView
          leftButtonImageName:(NSString *)leftButtonImageName
         rightButtonImageName:(NSString *)rightButtonImageName
                        style:(NSInteger)style
                 autoDissmiss:(BOOL)autoDissmiss
                  tapDissmiss:(BOOL)tapDissmiss {

    if (self = [super init]) {
        _title = title;
        _detailTitle = detailTitle;
        _contentView = contentView;
        _leftButtonImageName = leftButtonImageName;
        _rightButtonImageName = rightButtonImageName;
        _autoDissmiss = autoDissmiss;
        _tapDissmss = tapDissmiss;
        _style = style;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.frame = UIScreen.mainScreen.bounds;

    UIView *backgroundView = [[UIView alloc] initWithFrame:self.frame];
    backgroundView.backgroundColor = [UIColor.mainBgColor colorWithAlphaComponent:0.85];
    [self addSubview:backgroundView];
    _backgroundView = backgroundView;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapBackgroundView:)];
    [backgroundView addGestureRecognizer:tap];

    UIView *alertView = [UIView new];
    alertView.backgroundColor = [[UIColor mainBgColor] jk_lightenColor:0.05];
    alertView.layer.cornerRadius = 16;
    [self addSubview:alertView];
    _alertView = alertView;

    CGFloat width = 300;
    UIView *last = nil;

    if (self.title) {
        UILabel *titleLabel = [UILabel new];
        titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
        titleLabel.text = self.title;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.numberOfLines = 0;
        CGFloat labelWidth = width - 24;
        CGFloat labelHeight = [titleLabel sizeThatFits:CGSizeMake(labelWidth, CGFLOAT_MAX)].height;
        titleLabel.frame = CGRectMake(12, 32, labelWidth, labelHeight);
        [alertView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        last = titleLabel;
    }

    if (self.detailTitle) {
        UILabel *detailLabel = [UILabel new];
        detailLabel.font = [UIFont systemFontOfSize:14];
        detailLabel.text = self.detailTitle;
        detailLabel.textAlignment = NSTextAlignmentCenter;
        detailLabel.numberOfLines = 0;
        CGFloat contentWidth = width - 32;
        CGFloat detailHeight = [self.detailTitle jk_heightWithFont:detailLabel.font constrainedToWidth:contentWidth];
        detailLabel.frame = CGRectMake(16, CGRectGetMaxY(last.frame) + 16, contentWidth, detailHeight);
        [alertView addSubview:detailLabel];
        self.detailLabel = detailLabel;
        last = detailLabel;
    }


    if (_contentView) {
        _contentView.frame = CGRectMake(_contentView.jk_left, CGRectGetMaxY(last.frame) + 12, _contentView.jk_width, _contentView.jk_height);
        [alertView addSubview:_contentView];
        last = _contentView;
    }

    [self setupButtonsWithLastView:last inAlertView:alertView width:width];
}

- (void)setupButtonsWithLastView:(UIView *)last inAlertView:(UIView *)alertView width:(CGFloat)width {
    @weakify(self)

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn = leftBtn;
    [alertView addSubview:leftBtn];

    if (self.style == 0) {
        NSString *leftImage = self.leftButtonImageName.length > 0 ? self.leftButtonImageName : @"ic_close";
        [leftBtn setImage:[UIImage imageNamed:leftImage] forState:UIControlStateNormal];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.bottom.mas_offset(-20);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
    } else {
        [leftBtn setTitle:self.leftButtonImageName forState:UIControlStateNormal];
        [leftBtn setTitleColor:UIColor.darkGrayColor forState:UIControlStateNormal];
        leftBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.right.equalTo(alertView.mas_centerX).offset(-15);
            make.bottom.mas_offset(-20);
            make.height.mas_equalTo(30);
        }];
    }

    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.autoDissmiss) {
            [self dismss];
        }
        if (self.didClickIndexBlock) self.didClickIndexBlock(0);
    }];

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn = rightBtn;
    [alertView addSubview:rightBtn];

    if (self.style == 0) {
        NSString *rightImage = self.rightButtonImageName.length > 0 ? self.rightButtonImageName : @"ic_gouzi";
        [rightBtn setImage:[UIImage imageNamed:rightImage] forState:UIControlStateNormal];
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-20);
            make.bottom.mas_offset(-20);
            make.height.mas_equalTo(30);
            make.width.mas_greaterThanOrEqualTo(30);
        }];
    } else {
        [rightBtn setTitle:self.rightButtonImageName forState:UIControlStateNormal];
        [rightBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(alertView.mas_centerX).offset(15);
            make.right.mas_offset(-20);
            make.bottom.mas_offset(-20);
            make.height.mas_equalTo(30);
        }];
    }

    [[rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.autoDissmiss) {
            [self dismss];
        }
        if (self.didClickIndexBlock) self.didClickIndexBlock(1);
    }];

    CGFloat totalHeight = CGRectGetMaxY(last.frame) + 24 + 30 + 20;
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(totalHeight);
    }];
}

+ (CGFloat)alertWidth {
    return 300;
}

- (void)show {
    [self showInView:UIWindow.keyWindow.rootViewController.view];
}

- (void)showInView:(UIView *)view {
    [view addSubview:self];

    if (!self.superview) return;

    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.superview);
    }];

    self.alertView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    self.alertView.alpha = 0;
    self.alpha = 0;

    [UIView animateWithDuration:0.3 animations:^{
        self.alertView.transform = CGAffineTransformIdentity;
        self.alertView.alpha = 1;
        self.alpha = 1;
    }];
}

- (void)didTapBackgroundView:(id)sender {
    if (self.tapDissmss) {
        [self dismss];
    }
}

- (void)dismss {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
